﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PluginSdk;
using System;
using System.Collections;
using System.Xml;

namespace xDataSet
{
    public class SdkClass : IPluginSdk
    {
        public SdkClass(string pModel, string pVersion)
        {
            this.Model = pModel;
            this.Version = pVersion;
            this.XmlConfig = OperateXmlFile.f_ReadXmlFile(this.f_GetSysConfigFilePath(pModel, pVersion));
        }

        #region 接口实现


        #region redis操作

        /// <summary>
        /// 获取redis的string值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public object f_GetRedisString(string key)
        {
            return StackExchangeRedisHelper.GetString(Config.RedisCacheDBNo, Model + "_" + Version + "_" + key);
        }

        
        /// <summary>
        /// 设置redis的string值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expiretime">过期时间（单位：秒）</param>
        public void f_SetRedisString(string key, string value, int expiretime)
        {
            StackExchangeRedisHelper.SetString(Config.RedisCacheDBNo, Model + "_" + Version + "_" + key, value,0);
        }


        #endregion

        #region 方法

        public object f_Get(object key)
        {
            throw new NotImplementedException();
        }

        public void f_Set(object key, object value)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取运行平台信息（json信息字符串）
        /// </summary>
        /// <returns></returns>
        public string f_GetPlatInfo()
        {
            return "";
        }
        /// <summary>
        /// 获取运行环境根目录
        /// </summary>
        /// <returns></returns>
        public string f_GetRootPath()
        {
            JObject ob = new JObject();
            ob["PLATNAME"] = "";// Config.PlatName;
            ob["PLATVERSION"] = "";// Config.PlatVersion;
            ob["PLATROOTFLODER"] = "";// GetRootPath();
            ob["PLATENVIRONMENT"] = "";//Config.RunEnvironment;
            return JsonConvert.SerializeObject(ob);
        }
        /// <summary>
        /// 获取运行环境 windows/linux
        /// </summary>
        /// <returns></returns>
        public string f_GetRunEnvironment()
        {
            return "windows";
        }

        /// <summary>
        /// 获取默认配置文件路径
        /// </summary>
        /// <returns></returns>
        public string f_GetSysConfigFilePath(string model, string version)
        {
            return System.AppDomain.CurrentDomain.BaseDirectory + @"\sysConfig.xml";
        }
        /// <summary>
        /// 读取用户token值
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public JObject f_ReadUserToken(string userid)
        {
            try
            {
                
                return null;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 写入用户token值
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="token"></param>
        public void f_WriteTokenToCache(string userid, string token)
        {
            try
            {
                ;
            }
            catch
            {

            }
        }
        /// <summary>
        /// 将返回数据组织成JObject对象
        /// 注意：传入的数据对象仅限 Array,List,Hashtable,DataTable,string(String)等数据集合   
        /// </summary>
        /// <param name="ob"></param>
        /// <returns></returns>
        public JObject f_GetRetValue(object ob, bool NeedCache = false)
        {
            try
            {
                JObject retValue = new JObject();
                retValue["DLLCODE"] = "1";
                retValue["DATA"] = ToJToken(ob);
                retValue["CACHE"] = NeedCache.ToString();
                return retValue;
            }
            catch (Exception ex)
            {
                Exception e = new Exception("返回结果异常:" + ex.InnerException);
                throw e;
            }
        }
        /// <summary>
        /// 将传入参数  转换成为JObject对象
        /// </summary>
        /// <param name="ob">传入参数</param>
        /// <returns></returns>
        public JObject f_GetParams(object ob)
        {
            try
            {
                string str = Convert.ToString(ob);
                JObject param = JObject.Parse(str);
                return param;
            }
            catch
            {
                Exception e = new Exception("解析参数异常:" + ob);
                throw e;
            }
        }
        /// <summary>
        /// 将异常信息包装成JObject返回对象
        /// </summary>
        /// <param name="ex">异常信息</param>
        /// <returns></returns>
        public JObject f_GetExpcetionData(Exception ex)
        {
            JObject retValue = new JObject();
            retValue["DLLCODE"] = "-1";
            string message = "";
            if (ex != null)
            {
                message = ex.Message;
            }
            retValue["MESSAGE"] = message;
            return retValue;
        }

        private static JToken ToJToken(object ob)
        {
            JToken result;
            if (ob == null)
            {
                result = string.Empty;
            }
            else
            {
                switch (Type.GetTypeCode(ob.GetType()))
                {
                    case TypeCode.Empty:
                        {
                            result = string.Empty;
                            break;
                        }
                    case TypeCode.DBNull:
                        {
                            result = string.Empty;
                            break;
                        }
                    default:
                        {
                            result = JToken.FromObject(ob);
                            break;
                        }
                }
            }
            return result;
        }

        #endregion

        #endregion

        string mModel;

        public string Model
        {
            get
            {
                return mModel;
            }
            set
            {
                mModel = value;
            }
        }

        JToken IPluginSdk.ToJToken(object ob)
        {
            throw new NotImplementedException();
        }
        string mVersion;
        public string Version
        {
            get
            {
                return mVersion;
            }
            set
            {
                mVersion = value;
            }
        }

        XmlDocument mXmlConfig;
        public XmlDocument XmlConfig
        {
            get
            {
                return mXmlConfig;
            }
            set
            {
                mXmlConfig = value;
            }
        }

        public System.Collections.Hashtable f_GetOtherRunEnvironment()
        {
            return new Hashtable();
        }
    }
}